from flask import Blueprint
from flask_restx import Api

from app.role_control import (
    AccountManagementControl,
    AthleticsCompetitionControl,
    CombatMatchCompetitionControl,
    CtfCompetitionDrillControl,
    CtfMyQuestionBankListControl,
    CtfQuestionManagementControl,
    CTFRedBlueManageControl,
    CtfSystemQuestionBankListControl,
    DeleteVmEnvControl,
    HomeIndexControl,
    KnowledgeCoursesControl,
    LoadBlueprintControl,
    MemberBulkImportUserControl,
    MyCoursewareControl,
    ParLoadBlueprintControl,
    RangeManageRuleControl,
    RangeSceneListControl,
    RangeSceneSimulationControl,
    RangeSceneTemplateLibraryControl,
    RestoreFactorySettingsControl,
    RestoreFactorySettingsViewControl,
    SafetyKnowledgeControl,
    SystemCoursesControl,
    SystemCoursewareControl,
    SystemRestoreControl,
    UserManagementControl, HomeIndexRuleControl, HomeRangeInfoRuleControl, MemberManagerRuleControl,
    MemberPasswordRuleControl, MemberListRuleControl, RangeListRuleControl, RangeInfoRuleControl,
    RangeConfirmRuleControl, RangeTopologyRuleControl, RangeStartRuleControl, RangeCollectRuleControl,
    RangeVmManageRuleControl, RangeVmOperateRuleControl, RangeVmSnapshotRuleControl, RangeVmInfoRuleControl,
    RangeVmEnterRuleControl, RangeVmImageListRuleControl, SecurityGroupRuleControl, SecurityGroupListRuleControl,
    SecurityGroupAssociatedRuleControl, SecurityGroupRuleRuleControl, VmRangeInfosRuleControl,
    ActiveDeleteVmRuleControl, TaasRuleControl, TaasFlowRuleControl, SwitchNetworkRuleControl, ConnectDeviceRuleControl,
    SubnetPortRuleControl, VmPortBindingRuleControl, RouterRuleControl, SwitchCidrRuleControl, RouterTablesRuleControl,
    BindIssueRuleControl, BindIssueListRuleControl, ContestInfoRuleControl, CtfQuestionManageRuleControl,
    CtfQuestionListRuleControl, CtfMyQuestionListRuleControl, ClassManageRuleControl, ClassListRuleControl,
    ClassUserRuleControl, CourseWareListRuleControl, CourseWareManageRuleControl, CoursewareCollectRuleControl,
    CoursewareStatusRuleControl, CourseManageRuleControl, CourseListRuleControl, CourseJoinRuleControl,
    CourseStatusRuleControl, CTFmathRuleControl, CTFTempRangeLockRuleControl, CTFmathListRuleControl,
    CTFmathRangeRuleControl, CTFmathStatusRuleControl, CTFmonitorRuleControl, CTFmathQuestionRuleControl,
    CTFmathUserRuleControl, CTFmathScoreRuleControl, CTFmathListScoreRuleControl, CTFRedBlueManageListRuleControl,
    CTFRedBlueCloseRuleControl, CTFRedBlueScoreRuleControl, CTFRedBlueScreenRuleControl, CTFManualJudgmentRuleControl,
    RedBlueRangeScoreInfoRuleControl, AllRangeVmOperateRuleControl, CTFNoticeInfo, CTFRedBlueQuestionCountControl,
    CTFRedBlueScoreCompareControl, OneStudentCTFmathListScoreRuleControl, CTFRedBlueScoreInfoRuleControl,
)

# # 组织者路由集
# apiBlueprint = Blueprint("api", __name__, url_prefix="/v1/organizer")
# api = Api(apiBlueprint)

# 首页模块
homeIndex = Blueprint(
    "home_index", __name__, url_prefix="/v1/organizer", role_control=HomeIndexControl
)
home_index = Api(homeIndex)

# 用户管理模块
userManagement = Blueprint(
    "user_management",
    __name__,
    url_prefix="/v1/organizer",
    role_control=UserManagementControl,
)
# 账号管理模块
accountManagement = Blueprint(
    "account_management", __name__, role_control=AccountManagementControl
)
userManagement.register_blueprint(accountManagement)
account_management = Api(accountManagement)

# 场景仿真模块
rangeSceneSimulation = Blueprint(
    "range_scene_simulation",
    __name__,
    url_prefix="/v1/organizer",
    role_control=RangeSceneSimulationControl,
)
# 场景列表模块
rangeSceneList = Blueprint(
    "range_scene_list", __name__, role_control=RangeSceneListControl
)
rangeSceneSimulation.register_blueprint(rangeSceneList)
range_scene_list = Api(rangeSceneList)
# 场景模板库模块
rangeSceneTemplateLibrary = Blueprint(
    "range_scene_template_library",
    __name__,
    role_control=RangeSceneTemplateLibraryControl,
)
rangeSceneSimulation.register_blueprint(rangeSceneTemplateLibrary)
range_scene_template_library = Api(rangeSceneTemplateLibrary)

# 题库管理模块
ctfQuestionManagement = Blueprint(
    "ctf_question_management",
    __name__,
    url_prefix="/v1/organizer",
    role_control=CtfQuestionManagementControl,
)
# 系统题库
ctfSystemQuestionBankList = Blueprint(
    "ctf_system_question_bank_list",
    __name__,
    role_control=CtfSystemQuestionBankListControl,
)
ctfQuestionManagement.register_blueprint(ctfSystemQuestionBankList)
ctf_system_question_bank_list = Api(ctfSystemQuestionBankList)
# 我的题库
CtfMyQuestionBankList = Blueprint(
    "ctf_my_question_bank_list", __name__, role_control=CtfMyQuestionBankListControl
)
ctfQuestionManagement.register_blueprint(CtfMyQuestionBankList)
ctf_my_question_bank_list = Api(CtfMyQuestionBankList)

# 安全知识模块
safetyKnowledge = Blueprint(
    "safety_knowledge",
    __name__,
    url_prefix="/v1/organizer",
    role_control=SafetyKnowledgeControl,
)
safety_knowledge = Api(safetyKnowledge)
# 系统课题
systemCourses = Blueprint("system_courses", __name__, role_control=SystemCoursesControl)
safetyKnowledge.register_blueprint(systemCourses)
system_courses = Api(systemCourses)
# 知识课题
knowledgeCourses = Blueprint(
    "knowledge_courses", __name__, role_control=KnowledgeCoursesControl
)
safetyKnowledge.register_blueprint(knowledgeCourses)
knowledge_courses = Api(knowledgeCourses)
# 系统课件
systemCourseware = Blueprint(
    "system_courseware", __name__, role_control=SystemCoursewareControl
)
safetyKnowledge.register_blueprint(systemCourseware)
system_courseware = Api(systemCourseware)
# 我的课件
myCourseware = Blueprint("my_courseware", __name__, role_control=MyCoursewareControl)
safetyKnowledge.register_blueprint(myCourseware)
my_courseware = Api(myCourseware)

# 竞技演练模块
ctfCompetitionDrill = Blueprint(
    "ctf_competition_drill",
    __name__,
    url_prefix="/v1/organizer",
    role_control=CtfCompetitionDrillControl,
)
# 竞技比赛模块
athleticsCompetition = Blueprint(
    "athletics_competition", __name__, role_control=AthleticsCompetitionControl
)
ctfCompetitionDrill.register_blueprint(athleticsCompetition)
athletics_competition = Api(athleticsCompetition)
# 红蓝对抗模块
combatMatchCompetition = Blueprint(
    "combat_match_competition", __name__, role_control=CombatMatchCompetitionControl
)
ctfCompetitionDrill.register_blueprint(combatMatchCompetition)
combat_match_competition = Api(combatMatchCompetition)

# 文件上传下载
loadBlueprint = Blueprint(
    "load", __name__, url_prefix="/v1/organizer", role_control=LoadBlueprintControl
)
loadBlueprint_par = Blueprint(
    "load_par",
    __name__,
    url_prefix="/v1/participant",
    role_control=ParLoadBlueprintControl,
)

# 系统设置模块
restoreFactorySettings = Blueprint(
    "restore_factory_settings",
    __name__,
    url_prefix="/v1/organizer",
    role_control=RestoreFactorySettingsControl,
)

# 系统还原模块
systemRestore = Blueprint("system_restore", __name__, role_control=SystemRestoreControl)
restoreFactorySettings.register_blueprint(systemRestore)
system_restore = Api(systemRestore)

from . import (  # noqa
    classes,
    connectDevice,
    course,
    courseware,
    home,
    load,
    mathManage,
    member,
    question,
    rangeEnv,
    rangeVm,
    switchNetwork,
    taas
)

# ########################################## 首页模块 #################################################
# 首页接口
home_index.add_resource(home.HomeIndex, "/home", role_control=HomeIndexRuleControl)
# 首页场景仿真
home_index.add_resource(home.HomeRangeInfo, "/home/range_info", role_control=HomeRangeInfoRuleControl)

# ########################################## 用户管理模块 #################################################
# 账号管理
account_management.add_resource(
    member.MemberManage, "/member", "/member/<int:id>",
    role_control=MemberManagerRuleControl
)
account_management.add_resource(
    member.MemberPassword, "/memberPwd/<int:id>",
    role_control=MemberPasswordRuleControl
)
# 获取参与者账户列表分页
account_management.add_resource(
    member.MemberList, "/member/list", endpoint="member/list",
    role_control=MemberListRuleControl,
)
account_management.add_resource(
    member.UsersBulkOperationsAPI,
    "/users/bulk/operations",
    role_control=MemberBulkImportUserControl,
)

# ########################################## 场景仿真模块 #################################################

# 获取靶场列表分页
range_scene_list.add_resource(
    rangeEnv.RangeList, "/range/list", endpoint="range/list",
    role_control=RangeListRuleControl,
)
# 靶场详细信息 包括vm
range_scene_list.add_resource(
    rangeEnv.RangeInfo, "/range/info",
    role_control=RangeInfoRuleControl,
)
# 靶场详细信息 包括vm
range_scene_list.add_resource(
    rangeEnv.RangeManage,
    "/range/manage/<string:range_uuid>",
    "/range/manage",
    role_control=RangeManageRuleControl,
)
# 场景生成资源栈
range_scene_list.add_resource(
    rangeEnv.RangeStackTemplate, 
    "/range/stack/template",
    "/range/stack/template/<int:stack_id>",
    role_control=RangeManageRuleControl
)

# 靶场确认保存接口
range_scene_list.add_resource(
    rangeEnv.RangeConfirm, "/range/confirm",
    role_control=RangeConfirmRuleControl,
)
# 靶场拓扑保存接口
range_scene_list.add_resource(
    rangeEnv.RangeTopology, "/range/topology",
    role_control=RangeTopologyRuleControl,
)

# 靶场开启接口
range_scene_list.add_resource(
    rangeEnv.RangeStart, "/range/start",
    role_control=RangeStartRuleControl,
)
# 靶场复制
range_scene_template_library.add_resource(
    rangeEnv.RangeCollect, "/range/collect",
    role_control=RangeCollectRuleControl,
)


# 靶场详细信息 包括vm
range_scene_list.add_resource(
    rangeVm.VmManage, "/vm/manage/<string:range_id>", "/vm/manage",
    role_control=RangeVmManageRuleControl,
)
# 开关机 删除虚机
range_scene_list.add_resource(
    rangeVm.VmOperate, "/vm/operate",
    role_control=RangeVmOperateRuleControl,
)

# 一键开启场景虚机
range_scene_list.add_resource(
    rangeVm.AllVmOperate, "/all/vm/operate",
    role_control=AllRangeVmOperateRuleControl,
)

# 虚机 快照 功能
range_scene_list.add_resource(
    rangeVm.VmSnapshot,
    "/vm/snapshot",
    "/vm/snapshot/<string:range_uuid>/<string:vm_uuid>",
    role_control=RangeVmSnapshotRuleControl,
)

# 取主机详细信息页 cpu 内存 硬盘 网络 等详细信息
range_scene_list.add_resource(
    rangeVm.VmInfo, "/vm/info",
    role_control=RangeVmInfoRuleControl,
)
# 进入虚拟机
range_scene_list.add_resource(
    rangeVm.VmEnter, "/vm/enter",
    role_control=RangeVmEnterRuleControl,
)
# 获取镜像系统列表
range_scene_list.add_resource(
    rangeVm.VmImageList, "/vm/imageList",
    role_control=RangeVmImageListRuleControl,
)
# 创建网络
# api.add_resource(rangeVm.VmNetwork, '/vm/network')
# 靶场环境
range_scene_list.add_resource(
    rangeVm.VmEnv, "/vm/vmEvn", role_control=DeleteVmEnvControl
)
# 安全组
range_scene_list.add_resource(
    rangeVm.SecurityGroup,
    "/vm/security_groups",
    "/vm/security_groups/<string:group_id>",
    role_control=SecurityGroupRuleControl
)
range_scene_list.add_resource(
    rangeVm.SecurityGroupList, "/vm/security_groups/list/<string:vm_uuid>",
    role_control=SecurityGroupListRuleControl
)
range_scene_list.add_resource(
    rangeVm.SecurityGroupAssociated,
    "/vm/security_groups_associated",
    "/vm/security_groups_associated/<string:vm_uuid>",
    role_control=SecurityGroupAssociatedRuleControl
)
# 安全组规则
range_scene_list.add_resource(
    rangeVm.SecurityGroupRule,
    "/vm/security_group_rules",
    "/vm/security_group_rules/<string:rule_id>",
    role_control=SecurityGroupRuleRuleControl
)

# 获取靶场下面虚机详细信息
range_scene_list.add_resource(
    rangeVm.VmRangeInfos, "/vm/rangeVmInfos", "/vm/rangeVmInfos/<string:range_uuid>",
    role_control=VmRangeInfosRuleControl
)

# 删除指定靶场下的虚拟机
range_scene_list.add_resource(
    rangeVm.ActiveDeleteVm, "/range/activeRange/delete",
    role_control=ActiveDeleteVmRuleControl
)
# tap
range_scene_list.add_resource(
    rangeVm.Taas, "/taas", "/taas/<string:range_uuid>",
    role_control=TaasRuleControl
)

# 创建删除flow
range_scene_list.add_resource(
    taas.TaasFlow, "/taas/flow",
    role_control=TaasFlowRuleControl
)

# 创建获取删除交换机
range_scene_list.add_resource(
    switchNetwork.SwitchNetwork,
    "/switch",
    "/switch/<string:range_uuid>/<string:switch_id>",
    role_control=SwitchNetworkRuleControl
)

# 连接线接口
range_scene_list.add_resource(
    connectDevice.ConnectDevice, "/connect/device",
    role_control=ConnectDeviceRuleControl
)
# 给交换机创建port
range_scene_list.add_resource(
    switchNetwork.SubnetPort, "/ports",
    role_control=SubnetPortRuleControl
)
# 虚拟机在开机时可选的端口
range_scene_list.add_resource(
    switchNetwork.VmPortBinding,
    "/vmports/<string:range_uuid>/<string:vm_uuid>/<string:subnet_id>",
    role_control=VmPortBindingRuleControl
)
# 创建路由
range_scene_list.add_resource(
    switchNetwork.Router, "/router", "/router/<string:router_id>",
    role_control=RouterRuleControl
)
# 查询当前靶场可用cidr
range_scene_list.add_resource(
    switchNetwork.SwitchCidr, "/switch/cidr/<string:range_uuid>",
    role_control=SwitchCidrRuleControl
)
# 创建静态路由表
range_scene_list.add_resource(
    switchNetwork.RouterTables,
    "/router_static_table",
    "/router_static_table/<string:router_id>",
    role_control=RouterTablesRuleControl
)

# 靶场模板赛题信息接口
range_scene_template_library.add_resource(
    rangeEnv.ContestInfo,
    "/range/template/contest_info",
    role_control=ContestInfoRuleControl
)
# 靶场拓扑页赛题绑定提交接口
range_scene_list.add_resource(
    rangeVm.BindIssue,
    "/range/vm/issue/bind",
    role_control=BindIssueRuleControl
)
# 靶场拓扑页绑定赛题查看已绑定赛题接口
range_scene_list.add_resource(
    rangeVm.BindIssueList,
    "/range/vm/issue/bind_list",
    role_control=BindIssueListRuleControl
)

# ########################################## 题库管理模块 #################################################
# ctf题库管理
ctf_system_question_bank_list.add_resource(
    question.CtfQuestionManage, "/question/ctfQues", "/question/ctfQues/<int:id>",
    role_control=CtfQuestionManageRuleControl
)
# 列表
ctf_system_question_bank_list.add_resource(
    question.CtfQuestionList, "/question/ctflist", endpoint="question/ctflist",
    role_control=CtfQuestionListRuleControl
)
# 获取不同场景下我的赛题列表
ctf_my_question_bank_list.add_resource(
    question.CtfMyQuestionList, "/question/my_list", endpoint="question/my_list",
    role_control=CtfMyQuestionListRuleControl
)

# ########################################## 安全知识模块 #################################################
# 班级管理
safety_knowledge.add_resource(
    classes.ClassManage, "/class", "/class/<int:id>",
    role_control=ClassManageRuleControl
)
safety_knowledge.add_resource(
    classes.ClassList, "/class/list", endpoint="class/list",
    role_control=ClassListRuleControl
)
safety_knowledge.add_resource(
    classes.ClassUser, "/class/user",
    role_control=ClassUserRuleControl
)

# 课件管理
safety_knowledge.add_resource(
    courseware.CourseWareList, "/courseware/list", endpoint="courseware/list",
    role_control=CourseWareListRuleControl
)
safety_knowledge.add_resource(
    courseware.CourseWareManage, "/courseware", "/courseware/<int:id>",
    role_control=CourseWareManageRuleControl
)
safety_knowledge.add_resource(
    courseware.CoursewareCollect,
    "/courseware/collect",
    "/courseware/collect/list",
    endpoint="courseware/collect/list",
    role_control=CoursewareCollectRuleControl
)
# 开启课件
safety_knowledge.add_resource(
    courseware.CoursewareStatus, "/courseware/status",
    role_control=CoursewareStatusRuleControl
)

# 课程管理
safety_knowledge.add_resource(
    course.CourseManage, "/course", "/course/<string:uuid>",
    role_control=CourseManageRuleControl
)
safety_knowledge.add_resource(
    course.CourseList, "/course/list", endpoint="course/list",
    role_control=CourseListRuleControl
)
safety_knowledge.add_resource(
    course.CourseJoin, "/course/join_course",
    role_control=CourseJoinRuleControl
)
# 开启课题(课程)
safety_knowledge.add_resource(
    course.CourseStatus, "/course/status",
    role_control=CourseStatusRuleControl
)

# ########################################## 竞技演练模块 #################################################
# ctf比赛
athletics_competition.add_resource(
    mathManage.CTFmath, "/math/ctf", "/math/ctf/<int:ctf_id>",
    role_control=CTFmathRuleControl
)
athletics_competition.add_resource(
    mathManage.CTFTempRangeLock, "/math/ctf/temp_ctf_range_lock",
    role_control=CTFTempRangeLockRuleControl
)
# ctf列表分页
athletics_competition.add_resource(
    mathManage.CTFmathList, "/math/ctflist", endpoint="math/ctflist",
    role_control=CTFmathListRuleControl
)
# ctf 关联靶场range  该页面可能已废弃
athletics_competition.add_resource(
    mathManage.CTFmathRange, "/math/ctfRange/<int:ctf_id>",
    role_control=CTFmathRangeRuleControl
)
# ctf 开启 关闭ctf比赛
athletics_competition.add_resource(
    mathManage.CTFmathStatus, "/math/ctfStatus/<int:ctf_id>",
    role_control=CTFmathStatusRuleControl
)
# ctf 大屏
athletics_competition.add_resource(
    mathManage.CTFmonitor, "/math/ctfMonitor/<int:ctf_id>",
    role_control=CTFmonitorRuleControl
)

# ctf比赛关卡及课题获取接口
athletics_competition.add_resource(
    mathManage.CTFmathQuestion, "/math/ctfQuestion/<int:ctf_id>",
    role_control=CTFmathQuestionRuleControl
)
# 获取某ctf比赛 用户列表
athletics_competition.add_resource(
    mathManage.CTFmathUser, "/math/ctfUser/<int:ctf_id>",
    role_control=CTFmathUserRuleControl
)
athletics_competition.add_resource(
    mathManage.CTFmathScore, "/math/ctfScore/<int:ctf_id>/<int:user_id>",
    role_control=CTFmathScoreRuleControl
)
athletics_competition.add_resource(
    mathManage.CTFmathListScore, "/math/ctfListScore/<int:ctf_id>",
    role_control=CTFmathListScoreRuleControl
)

athletics_competition.add_resource(
    mathManage.OneStudentCTFmathListScore, "/math/OneStudentCTFmathListScore/<int:ctf_id>/<int:user_id>",
    role_control=OneStudentCTFmathListScoreRuleControl
)
# ctf红蓝比赛
combat_match_competition.add_resource(
    mathManage.CTFRedBlueManage,
    "/math/ctf_red_blue",
    "/math/ctf_red_blue/<int:ctf_red_blue_id>",
    role_control=CTFRedBlueManageControl,
)
# ctf红蓝比赛列表
combat_match_competition.add_resource(
    mathManage.CTFRedBlueManageList,
    "/math/ctf_red_blue_list",
    endpoint="math/ctf_red_blue_list",
    role_control=CTFRedBlueManageListRuleControl,
)

# ctf红蓝比赛公告
combat_match_competition.add_resource(
    mathManage.CTFRedBlueNotice,
    "/math/ctf_notice",
    endpoint="/math/ctf_notice",
    role_control=CTFNoticeInfo,
)

# ctf 红蓝关闭比赛
combat_match_competition.add_resource(
    mathManage.CTFRedBlueClose,
    "/math/ctf_red_blue_close",
    endpoint="/math/ctf_red_blue_close",
    role_control=CTFRedBlueCloseRuleControl,
)
# 红蓝方实时成绩（所有进行中红蓝比赛成绩/某比赛所有团队成绩/某团队成员成绩）
combat_match_competition.add_resource(
    mathManage.CTFRedBlueScore,
    "/math/ctf_red_blue_score",
    "/math/ctf_red_blue_score/<int:ctf_red_blue_id>",
    "/math/ctf_red_blue_score/<int:ctf_red_blue_id>/<int:ctf_red_blue_group_id>",
    role_control=CTFRedBlueScoreRuleControl,
)

combat_match_competition.add_resource(
    mathManage.CTFRedBlueScoreInfo,
    "/math/ctf_red_blue_score_info/<int:ctf_red_blue_id>",
    role_control=CTFRedBlueScoreInfoRuleControl,
)

# ctf 大屏
combat_match_competition.add_resource(
    mathManage.CTFRedBlueScreen, "/math/ctf_red_blue_screen/<int:ctf_id>",
    role_control=CTFRedBlueScreenRuleControl,
)

# ctf大屏统计已答赛题统计
combat_match_competition.add_resource(
    mathManage.CTFRedBlueQuestionCount,
    "/math/ctf_red_blue_question_count/<int:ctf_red_blue_id>",
    role_control=CTFRedBlueQuestionCountControl,
)

# ctf红蓝每隔一小时比分比较
combat_match_competition.add_resource(
    mathManage.CTFRedBlueScoreCompare,
    "/math/ctf_red_blue_score_compare/<int:ctf_red_blue_id>",
    role_control=CTFRedBlueScoreCompareControl,
)

# 人工裁决申请列表/人工裁决申请详情
combat_match_competition.add_resource(
    mathManage.CTFManualJudgment,
    "/math/ctf_manual_judgment",
    "/math/ctf_manual_judgment/<int:ctf_red_blue_id>",
    role_control=CTFManualJudgmentRuleControl,
)
# 导调查看比赛记分规则
combat_match_competition.add_resource(
    rangeEnv.RedBlueRangeScoreInfo,
    "/math/red_blue_range_score_info/<string:range_uuid>",
    role_control=RedBlueRangeScoreInfoRuleControl,
)

# ########################################## 系统设置模块 #################################################

# 恢复出厂设置
system_restore.add_resource(
    rangeVm.RestoreFactorySettings,
    "/range/restoreFactorySettings",
    role_control=RestoreFactorySettingsViewControl,
)
